package cn.kgc.model.service.impl;

import cn.kgc.common.utils.constants.Constants;
import cn.kgc.common.utils.reids.RedisUtils;
import cn.kgc.model.entity.Classify;
import cn.kgc.model.entity.Model;
import cn.kgc.model.entity.dto.BrandDto;
import cn.kgc.model.entity.dto.ClassifyDto;
import cn.kgc.model.entity.dto.ModelDto;
import cn.kgc.model.mapper.BrandMapper;
import cn.kgc.model.mapper.ClassifyMapper;
import cn.kgc.model.mapper.ModelMapper;
import cn.kgc.model.service.ClassifyService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author qf
 * @since 2021-08-04
 */
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {

    private final ClassifyMapper classifyMapper;
    private final BrandMapper brandMapper;
    private final ModelMapper modelMapper;
    private final RedisUtils redisUtils;

    public ClassifyServiceImpl(ClassifyMapper classifyMapper, BrandMapper brandMapper, ModelMapper modelMapper, RedisUtils redisUtils) {
        this.classifyMapper = classifyMapper;
        this.brandMapper = brandMapper;
        this.modelMapper = modelMapper;
        this.redisUtils = redisUtils;
    }

    /*@Override
    public List<ClassifyDto> getClassifyTree() {
        // 1.获取所有分类
        List<ClassifyDto> list = classifyMapper.selectListDto();
        if (list != null) {
            for (ClassifyDto classifyDto : list) {
                Long cid = classifyDto.getId();
                // 2.根据分离id获取所有品牌
                List<BrandDto> brandDtoList = brandMapper.getBrandDtoListByClassifyId(cid);
                if (brandDtoList != null) {
                    for (BrandDto brandDto : brandDtoList) {
                        QueryWrapper<Model> wrapper = new QueryWrapper<>();
                        wrapper.eq("classifyId", cid)
                                .eq("brandId", brandDto.getId());
                        // 3.获取所有商品类型
                        List<ModelDto> modelDtoList = modelMapper.getModelDtoListByBrandId(wrapper);
                        brandDto.setModelList(modelDtoList);
                    }
                    classifyDto.setBrandList(brandDtoList);
                }
            }
        }
        return list;
    }*/
    @Override
    public List<ClassifyDto> getClassifyTree() {
        List<ClassifyDto> list = null;
        // 1.从缓存中获取数据
        String value = redisUtils.getValue(Constants.Redis.KEY_HOME_TREE);
        // 如果缓存数据为空
        if (StringUtils.isBlank(value)) {
            // 1.获取所有分类
            list = classifyMapper.selectListDto();
            if (list != null) {
                for (ClassifyDto classifyDto : list) {
                    Long cid = classifyDto.getId();
                    // 2.根据分类id获取所有品牌
                    List<BrandDto> brandDtoList = brandMapper.getBrandDtoListByClassifyId(cid);
                    if (brandDtoList != null) {
                        for (BrandDto brandDto : brandDtoList) {
                            QueryWrapper<Model> wrapper = new QueryWrapper<>();
                            wrapper.eq("classifyId", cid)
                                    .eq("brandId", brandDto.getId());
                            // 3.获取所有商品类型
                            List<ModelDto> modelDtoList = modelMapper.getModelDtoListByBrandId(wrapper);
                            brandDto.setModelList(modelDtoList);
                        }
                        classifyDto.setBrandList(brandDtoList);
                    }
                }
                // 将list放入缓存中
                redisUtils.putValue(Constants.Redis.KEY_HOME_TREE, JSONObject.toJSONString(list));
            }
        } else {
            // 不为空
            list = JSON.parseArray(value, ClassifyDto.class);
        }
        return list;
    }
}
